package jp.co.cos.ittrick.channel.controller.admin.article;

import jp.co.cos.ittrick.channel.model.ArticleModel;
import jp.co.cos.ittrick.channel.service.ArticleService;
import jp.co.cos.ittrick.channel.service.FileService;

import org.slim3.controller.Controller;
import org.slim3.controller.Navigation;

public class ArticleEditController extends Controller {

    private ArticleService articleService;
    private FileService fileService;

    public ArticleEditController() {
        articleService = new ArticleService();
        fileService = new FileService();
    }

    @Override
    public Navigation run() throws Exception {

        // リクエストパラメータ
        String article = asString("article");

        ArticleModel articleModel = articleService.getArticleByKey(Integer.valueOf(article));
        requestScope("articleModel", articleModel);
        requestScope("article", article);

        // キーワード
        String keyWord = null;
        for(String keyWordTmp: articleModel.getKeyWordList()) {
            if(keyWord == null) {
                keyWord = keyWordTmp;
            }else {
                keyWord = keyWord + "," + keyWordTmp;
            }
        }
        requestScope("keyWord", keyWord);

        // ファイル
        requestScope("dataList", fileService.getDataListOfArticle(Integer.valueOf(article)));

        return forward("articleEdit.jsp");
    }
}
